package logc.util;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeUtility;

/**
 * Gmail Send
 * @author mhko
 *
 */
public class MailUtil implements Runnable {
	
	private String subject; 
	private String body;
	private String username;
	private String password;
	private String sMail;
	private String rMails;
	
	/**
	 * GMAIL 계정으로 메일 발송
	 * @param subject - 제목
	 * @param body - 내용
	 * @param rMails - 받는사람 메일주소(N)
	 * @throws MessagingException
	 */
	//public MailUtil(String subject, String body, String username, String password, String sMail, String rMail){
	public MailUtil(String subject, String body, String rMails){
		this.subject = subject;
		this.body = body;
		this.username = "canella79@gmail.com";//GeneralUtil.getData(values, "username", "아이디", false);
		this.password = "tkvk7197";//GeneralUtil.getData(values, "password", "비밀번호", false);
		this.sMail = "canella79@gmail.com";//GeneralUtil.getData(values, "sMail", "보내는사람메일주소", false);
		this.rMails = rMails;
	}
	
	

	private Session getSession(String username, String password) {
		Authenticator authenticator = new Authenticator(username, password);

		Properties properties = new Properties();
		properties.setProperty("mail.smtp.starttls.enable", "true");	// 첫번째로 starttls 속성이 set 되어야 함.
		properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.host", "smtp.gmail.com");
		properties.setProperty("mail.smtp.port", "587");

		return Session.getInstance(properties, authenticator);
	}

	private class Authenticator extends javax.mail.Authenticator {
		private PasswordAuthentication authentication;

		public Authenticator(String username, String password) {
			authentication = new PasswordAuthentication(username, password);
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return authentication;
		}
	}


	public void run() {
		
		try {
			
			String[] rMail = rMails.split(",");
			
			for(int i=0; i<rMail.length; i++){
				Message message = new MimeMessage(getSession(username, password));
	
			
				message.addRecipient(RecipientType.TO, new InternetAddress(rMail[i]));
				message.addFrom(new InternetAddress[] { new InternetAddress(sMail) });
				try {
					message.setSubject(MimeUtility.encodeText(subject,"utf-8","B"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				message.setContent(body, "text/html; charset=\"utf-8\"");
		
				Transport.send(message);
			}
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
